package com.hydra.base.auth;

import cn.hutool.core.collection.CollectionUtil;

import java.util.List;

/**
 * 用户角色枚举
 * @Author 陈煜敏
 * @Date 2022/9/2 16:31
 */
public enum UserRoleEnum {

    /**
     * 角色-超级管理员
     */
    ROLE_SU_ADMIN("ROLE_SU_ADMIN", 999),

    /**
     * 角色-管理员
     */
    ROLE_ADMIN("ROLE_ADMIN", 9),

    /**
     * 角色-用户
     */
    ROLE_USER("ROLE_USER", 3),

    /**
     * 角色-客户端用户
     */
    ROLE_APP_USER("ROLE_APP_USER", 2),

    /**
     * 角色-临时用户
     */
    ROLE_TEMPORARY_USER("ROLE_TEMPORARY_USER", 1),

    /**
     * 角色-最低权限
     */
    ROLE_ANONYMOUS("ROLE_ANONYMOUS", 0);

    UserRoleEnum(String code, int value){
        this.code = code;
        this.value = value;
    }

    String code;
    int value;

    public String getCode() {
        return code;
    }

    public int getValue() {
        return value;
    }


    public static UserRoleEnum find(String code){
        for (UserRoleEnum role : UserRoleEnum.values()) {
            if(role.getCode().equals(code)){
                return role;
            }
        }
        return ROLE_ANONYMOUS;
    }


    /**获取最小的角色*/
    public static UserRoleEnum findMaxRole(List<String> codes){
        if(CollectionUtil.isEmpty(codes)){
            return ROLE_ANONYMOUS;
        }
        UserRoleEnum currentRole = ROLE_ANONYMOUS;
        for (UserRoleEnum role : UserRoleEnum.values()) {
            for (String code : codes) {
                if(role.getCode().equals(code)){
                    if(role.value > currentRole.value){
                        currentRole = role;
                    }
                    break;
                }
            }
        }
        return currentRole;
    }

}
